//
//  SettingVC.swift
//  kxf
//
//  Created by Mac on 2024/3/15.
//

import Foundation
import UIKit
import MCToast


class SettingVC: WHBaseViewVC {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        initUI()
    }
    lazy var logoImgView : UIImageView = {
        let img = UIImageView()
        img.setImgLocal(imgName: "login_logo_img")
        
        return img
    }()
    lazy var nameLabel : UILabel = {
        let lab = UILabel()
        lab.text = "星渝付助手"
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 24, weight: .medium)
        
        return lab
    }()
    lazy var versionLabel : UILabel = {
        let lab = UILabel()
        let currentVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
        lab.text = "当前版本 \(currentVersion)"
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 14, weight: .medium)
        
        return lab
    }()
    lazy var updatePswVm : MineFuncItemVM = {
        let vm = MineFuncItemVM.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        vm.frame = CGRect.init(x: 0, y: 0, width: SCREEN_WIDHT, height: kFitWidth(56))
//        let vm = MineFuncItemVM.init(frame: CGRect.init(x: 0, y: getNavigationBarHeight()+kFitWidth(290), width: 0, height: 0))
//        vm.frame = CGRect.init(x: 0, y: getNavigationBarHeight()+kFitWidth(290), width: SCREEN_WIDHT, height: kFitWidth(56))
        vm.titleLabel.text = "修改登录密码"
        vm.tapBlock = {()in
            let vc = UpdatePSWVC()
            vc.isForget = false
            self.navigationController?.pushViewController(vc, animated: true)
        }
        return vm
    }()
    lazy var cancelAccountVm : MineFuncItemVM = {
        let vm = MineFuncItemVM.init(frame: CGRect.init(x: 0, y: self.updatePswVm.frame.maxY, width: 0, height: 0))
        vm.frame = CGRect.init(x: 0, y: self.updatePswVm.frame.maxY, width: SCREEN_WIDHT, height: kFitWidth(56))
        vm.titleLabel.text = "注销账户"
        vm.tapBlock = {()in
            self.presentAlertVc(confirmBtn: "注销", message: "注销账户会删除您的所有数据，您确定注销吗？", title: "温馨提示", cancelBtn: "取消", handler: { action in
                self.cancelAccountRequest()
            }, viewController: self)
        }
        return vm
    }()
    lazy var protocalVm : MineFuncItemVM = {
        let vm = MineFuncItemVM.init(frame: CGRect.init(x: 0, y: self.cancelAccountVm.frame.maxY, width: 0, height: 0))
        vm.frame = CGRect.init(x: 0, y: self.cancelAccountVm.frame.maxY, width: SCREEN_WIDHT, height: kFitWidth(56))
        vm.titleLabel.text = "隐私协议"
        vm.tapBlock = {()in
            let vc = WHCommonH5VC()
            vc.urlString = URL_H5_private as NSString
            self.navigationController?.pushViewController(vc, animated: true)
        }
        return vm
    }()
    lazy var protocalTwoVm : MineFuncItemVM = {
        let vm = MineFuncItemVM.init(frame: CGRect.init(x: 0, y: self.protocalVm.frame.maxY, width: 0, height: 0))
        vm.frame = CGRect.init(x: 0, y: self.protocalVm.frame.maxY, width: SCREEN_WIDHT, height: kFitWidth(56))
        vm.titleLabel.text = "展业规范"
        vm.tapBlock = {()in
            let vc = WHCommonH5VC()
            vc.urlString = URL_H5_zhanye_guifan as NSString
            self.navigationController?.pushViewController(vc, animated: true)
        }
        return vm
    }()
    lazy var settleVm : MineFuncItemVM = {
        let vm = MineFuncItemVM.init(frame: CGRect.init(x: 0, y: self.protocalTwoVm.frame.maxY, width: 0, height: 0))
        vm.frame = CGRect.init(x: 0, y: self.protocalTwoVm.frame.maxY, width: SCREEN_WIDHT, height: kFitWidth(56))
        vm.titleLabel.text = "结算管理"
        vm.tapBlock = {()in
            let vc = SettleManageVC()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        return vm
    }()
    lazy var logoutBtn : UIButton = {
        let btn = UIButton()
        btn.frame = CGRect.init(x: kFitWidth(31), y: SCREEN_HEIGHT - getBottomSafeAreaHeight()-kFitWidth(64), width: kFitWidth(316), height: kFitWidth(48))
        btn.setTitle("退出登录", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.setBackgroundImage(UIImage.init(named: "button_theme_bg"), for: .normal)
        btn.layer.cornerRadius = kFitWidth(24)
        btn.clipsToBounds = true
        btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .regular)
        btn.backgroundColor = .clear
        
        btn.addTarget(self, action: #selector(logoutAction), for: .touchUpInside)
        
        return btn
    }()
}

extension SettingVC{
    @objc func logoutAction(){
        self.presentAlertVc(confirmBtn: "退出登录", message: "是否退出当前账号？", title: "温馨提示", cancelBtn: "取消", handler: { action in
            UserDefaults.standard.setValue("", forKey: whIdentity)
            UserDefaults.standard.setValue("", forKey: whTokenHead)
            UserDefaults.standard.setValue("", forKey: whToken)
            UserDefaults.standard.setValue("", forKey: Authorization)
//            self.view.window?.rootViewController = LoginVC()
            
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            let newRootVC = LoginVC()
            appDelegate.switchRootViewController(to: newRootVC)
            
        }, viewController: self)
    }
}

extension SettingVC{
    func initUI() {
        initNavi(titleStr: "我的设置")
        view.backgroundColor = .COLOR_LIGHT_GREY
        
        view.addSubview(logoImgView)
        view.addSubview(nameLabel)
        view.addSubview(versionLabel)
        
        view.addSubview(scrollView)
        scrollView.backgroundColor = .white
        scrollView.frame = CGRect.init(x: 0, y: getNavigationBarHeight()+kFitWidth(290), width: SCREEN_WIDHT, height: logoutBtn.frame.minY-(getNavigationBarHeight()+kFitWidth(290)) - kFitWidth(20))
        scrollView.contentSize = CGSize.init(width: 0, height: self.protocalTwoVm.frame.maxY)
        
        scrollView.addSubview(updatePswVm)
        scrollView.addSubview(cancelAccountVm)
        scrollView.addSubview(protocalVm)
        scrollView.addSubview(protocalTwoVm)
//        scrollView.addSubview(settleVm)
        
        view.addSubview(logoutBtn)
        
        setConstrait()
    }
    func setConstrait(){
        logoImgView.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualToSuperview()
            make.top.equalTo(getNavigationBarHeight()+kFitWidth(54))
            make.width.height.equalTo(kFitWidth(130))
        }
        nameLabel.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualToSuperview()
            make.top.equalTo(logoImgView.snp.bottom).offset(kFitWidth(17))
            make.height.equalTo(kFitWidth(34))
        }
        versionLabel.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualToSuperview()
            make.top.equalTo(nameLabel.snp.bottom).offset(kFitWidth(11))
        }
    }
}


extension SettingVC{
    @objc func cancelAccountRequest() {
        MCToast.mc_loading()
        WHNetworkUtil.shareManager().POST(urlString: URL_cancel_account, parameters: nil,isNeedToast: true,vc: self) { responseObject in
            self.presentAlertVc(confirmBtn: "确定", message: "", title: "账户已注销成功！", cancelBtn: nil, handler: { action in
                UserDefaults.standard.setValue("", forKey: whIdentity)
                UserDefaults.standard.setValue("", forKey: whTokenHead)
                UserDefaults.standard.setValue("", forKey: whToken)
                UserDefaults.standard.setValue("", forKey: Authorization)
                
                let appDelegate = UIApplication.shared.delegate as! AppDelegate
                let newRootVC = LoginVC()
                appDelegate.switchRootViewController(to: newRootVC)
            }, viewController: self)
           
        }
    }
}
